package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import cn.com.ghy.android.util.XMLStatic;

import android.util.Xml;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:最新任务XML文件解析类
 * @author wuz  @date 2011-5-23 17:02:55
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class TaskBeanParser {
	public static List<TaskBean> parseXMLForTaskList(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();

		List<TaskBean> list = null;
		TaskBean bean = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<TaskBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						bean = new TaskBean();
					} else if (null != list && null != bean) {
						if ("TASKID".equals(parser.getName())) {
							bean.setTaskId(parser.nextText());
						} else if ("TITLE".equals(parser.getName())) {
							bean.setTitle(parser.nextText());
						} else if ("STATE".equals(parser.getName())) {
							bean.setState(parser.nextText());
						} else if ("CDATE".equals(parser.getName())) {
							bean.setCdate(parser.nextText().substring(0, 10));
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						if (null != list && null != bean) {
							list.add(bean);
							bean = null;
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}

	public static List<TaskBean> parseXMLForTaskDetail(InputStream in) throws Exception {
		XmlPullParser parser = Xml.newPullParser();
		parser.setInput(in, "UTF-8");
		int type = parser.getEventType();
		List<TaskBean> list = null;
		TaskBean bean = null;

		while (type != XmlPullParser.END_DOCUMENT) {
			switch (type) {
				case XmlPullParser.START_DOCUMENT :
					list = new ArrayList<TaskBean>();
					break;

				case XmlPullParser.START_TAG :
					if ("Table".equals(parser.getName())) {
						bean = new TaskBean();
					} else if (null != list && null != bean) {
						if ("TASKID".equals(parser.getName())) {
							bean.setTaskId(parser.nextText());
						} else if ("TITLE".equals(parser.getName())) {
							bean.setTitle(parser.nextText());
						} else if ("CONTENT".equals(parser.getName())) {
							bean.setContent(parser.nextText());
						} else if ("STATE".equals(parser.getName())) {
							bean.setState(parser.nextText());
						} else if ("CUSER".equals(parser.getName())) {
							bean.setCuser(parser.nextText());
						} else if ("CDATE".equals(parser.getName())) {
							bean.setCdate(parser.nextText().substring(0, 10));
						}
					}
					break;

				case XmlPullParser.END_TAG :
					if ("Table".equals(parser.getName())) {
						if (null != list && null != bean) {
							list.add(bean);
							bean = null;
						}
					}
					break;
			}
			type = parser.next();
		}
		in.close();
		return list;
	}

	public static String createXMLTaskhead(TaskBean taskBean, Writer writer) throws Exception {

		XmlSerializer xmlSerializer = Xml.newSerializer();

		xmlSerializer.setOutput(writer);
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
		xmlSerializer.startTag("", XMLStatic.RECORDS);

		xmlSerializer.startTag("", XMLStatic.TASKID);
		xmlSerializer.text(taskBean.getTaskId());
		xmlSerializer.endTag("", XMLStatic.TASKID);

		xmlSerializer.startTag("", XMLStatic.DEALCONTENT);
		xmlSerializer.text(taskBean.getDealcontent());
		xmlSerializer.endTag("", XMLStatic.DEALCONTENT);

		xmlSerializer.startTag("", XMLStatic.DEALUSER);
		xmlSerializer.text(taskBean.getDealuser());
		xmlSerializer.endTag("", XMLStatic.DEALUSER);

		xmlSerializer.startTag("", XMLStatic.STATE);
		xmlSerializer.text(taskBean.getState());
		xmlSerializer.endTag("", XMLStatic.STATE);

		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace(
				"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}

	public static String createXMLNoteheadX(WorkReportBean bean, Writer writer) throws Exception {

		XmlSerializer xmlSerializer = Xml.newSerializer();

		xmlSerializer.setOutput(writer);
		xmlSerializer.startDocument("UTF-8", true);
		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
		xmlSerializer.startTag("", XMLStatic.RECORDS);

		xmlSerializer.startTag("", XMLStatic.TITLE);
		xmlSerializer.text(bean.getTitle());
		xmlSerializer.endTag("", XMLStatic.TITLE);

		xmlSerializer.startTag("", XMLStatic.NOTETYPE);
		xmlSerializer.text(bean.getNotetype());
		xmlSerializer.endTag("", XMLStatic.NOTETYPE);

		xmlSerializer.startTag("", XMLStatic.CONTENT);
		xmlSerializer.text(bean.getContent());
		xmlSerializer.endTag("", XMLStatic.CONTENT);

		xmlSerializer.startTag("", XMLStatic.CUSER);
		xmlSerializer.text(bean.getCuser());
		xmlSerializer.endTag("", XMLStatic.CUSER);

		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace(
				"<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
}
